﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Security;
using System.Windows.Forms;
using BGShell.Components;
using BGShell.PowerShell.Host;

namespace BGShell.PowerShell.Controls
{
    partial class ConsoleControl : IRunspaceHostUI
    {
        IRunspaceHostRawUI IRunspaceHostUI.RawUI
        {
            get { return this; }
        }

        Dictionary<String, PSObject> IRunspaceHostUI.Prompt(string caption, string message, Collection<FieldDescription> descriptions)
        {
            throw new NotImplementedException();
        }

        Int32 IRunspaceHostUI.PromptForChoice(String caption, String message, Collection<ChoiceDescription> choices, Int32 defaultChoice)
        {
            throw new NotImplementedException();
        }

        PSCredential IRunspaceHostUI.PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
        {
            throw new NotImplementedException();
        }

        PSCredential IRunspaceHostUI.PromptForCredential(string caption, string message, string userName, string targetName)
        {
            throw new NotImplementedException();
        }

        void IRunspaceHostUI.SetState(RunspaceHostState state)
        {
            BeginInvokeIfRequired(delegate
            {
                var isOpened = (state == RunspaceHostState.Opened);
                var isClosed = (state == RunspaceHostState.Closed);

                CurrentStatus = ConsoleControlStatus.None;

                if (isClosed)
                {
                    OnClosed(new ConsoleControlClosedEventArgs(_runspaceProxy.GetExitCode()));
                    _runspaceProxy = null;
                }
            });
        }

        void IRunspaceHostUI.RegisterHookKeys(IEnumerable<Keys> keys)
        {
            BeginInvokeIfRequired(RegisterHookKeys, keys);
        }

        void IRunspaceHostUI.UnregisterHookKeys(IEnumerable<Keys> keys)
        {
            BeginInvokeIfRequired(UnregisterHookKeys, keys);
        }

        string IRunspaceHostUI.ReadLine()
        {
            return ReadLine();
        }

        SecureString IRunspaceHostUI.ReadLineAsSecureString()
        {
            throw new NotImplementedException();
        }

        void IRunspaceHostUI.Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            AppendBuffered(OutputItemType.Normal, value);
        }

        void IRunspaceHostUI.Write(string value)
        {
            AppendBuffered(OutputItemType.Normal, value);
        }

        void IRunspaceHostUI.WriteDebugLine(string message)
        {
            AppendBufferedLine(OutputItemType.Debug, message);
        }

        void IRunspaceHostUI.WriteErrorLine(string value)
        {
            AppendBufferedLine(OutputItemType.Error, value);
        }

        void IRunspaceHostUI.WriteLine(string value)
        {
            AppendBufferedLine(OutputItemType.Normal, value);
        }

        void IRunspaceHostUI.WriteProgress(long sourceId, ProgressRecord record)
        {
            throw new NotImplementedException();
        }

        void IRunspaceHostUI.WritePrompt(string prompt)
        {
            BeginInvokeIfRequired(WritePrompt, prompt);
        }

        void IRunspaceHostUI.WriteVerboseLine(string message)
        {
            AppendBufferedLine(OutputItemType.Verbose, message);
        }

        void IRunspaceHostUI.WriteWarningLine(string message)
        {
            AppendBufferedLine(OutputItemType.Warning, message);
        }

        private void AppendBuffered(OutputItemType type, string text)
        {
            _writer.AddItem(type, text);
        }

        private void AppendBufferedLine(OutputItemType type, string value)
        {
            AppendBuffered(type, value + Environment.NewLine);
        }

    }
}